import _regeneratorRuntime from "@babel/runtime/regenerator";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * DataHierarchies 数据层级配置
 * @date: 2019-8-14
 */
import request from 'utils/request';
import { CPAAS_PLATFORM } from 'utils/config';
import { getCurrentOrganizationId, isTenantRoleLevel } from 'utils/utils';
var organizationId = getCurrentOrganizationId();
var organizationRoleLevel = isTenantRoleLevel();
/**
 * 查询数据组列表
 * @param {any} params - 查询参数
 */

export function queryConfig() {
  return _queryConfig.apply(this, arguments);
}
/**
 * 查询数据组列表
 * @param {any} params - 查询参数
 */

function _queryConfig() {
  _queryConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var params,
        _args = arguments;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            params = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
            return _context.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId, "/data-hierarchies") : "".concat(CPAAS_PLATFORM, "/v1/data-hierarchies"), {
              method: 'GET',
              query: params
            }));

          case 2:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _queryConfig.apply(this, arguments);
}

export function getConfigDetail() {
  return _getConfigDetail.apply(this, arguments);
}
/**
 * 查询数据组列表
 * @param {any} params - 查询参数
 */

function _getConfigDetail() {
  _getConfigDetail = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var params,
        dataHierarchyId,
        tenantId,
        _args2 = arguments;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            params = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : {};
            dataHierarchyId = params.dataHierarchyId, tenantId = params.tenantId;
            return _context2.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId, "/data-hierarchies/").concat(dataHierarchyId) : "".concat(CPAAS_PLATFORM, "/v1/data-hierarchies/").concat(dataHierarchyId), {
              method: 'GET',
              query: {
                tenantId: tenantId
              }
            }));

          case 3:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _getConfigDetail.apply(this, arguments);
}

export function createConfig() {
  return _createConfig.apply(this, arguments);
}
/**
 * 查询数据组列表
 * @param {any} params - 查询参数
 */

function _createConfig() {
  _createConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    var params,
        _args3 = arguments;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            params = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : {};
            return _context3.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId, "/data-hierarchies") : "".concat(CPAAS_PLATFORM, "/v1/data-hierarchies"), {
              method: 'POST',
              body: params
            }));

          case 2:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _createConfig.apply(this, arguments);
}

export function editConfig() {
  return _editConfig.apply(this, arguments);
}
/**
 * 处理接口获取的数据，提取每个节点的层次路径
 * @param {array} collections - 页面展示数据
 * @param {array} levelPath - 特定组织的层级路径
 * @returns {object} 节点树和层次路径组成的对象
 */

function _editConfig() {
  _editConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
    var params,
        _args4 = arguments;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            params = _args4.length > 0 && _args4[0] !== undefined ? _args4[0] : {};
            return _context4.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId, "/data-hierarchies") : "".concat(CPAAS_PLATFORM, "/v1/data-hierarchies"), {
              method: 'PUT',
              body: params
            }));

          case 2:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _editConfig.apply(this, arguments);
}

export function renderTreeData() {
  var collections = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var levelPath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  // debugger;
  var pathMap = levelPath;
  var renderTree = collections.map(function (item) {
    var temp = item;
    pathMap[temp.dataHierarchyId] = [].concat(_toConsumableArray(pathMap[temp.parentid] || []), [temp.dataHierarchyId]);

    if (temp.children) {
      temp.children = _toConsumableArray(renderTreeData(temp.children || [], pathMap).renderTree);
    }

    return temp;
  });
  return {
    renderTree: renderTree,
    pathMap: pathMap
  };
}